//
//  RootController.m
//  CJPackOCDemo
//
//  Created by 陈警卫 on 2025/7/15.
//

#import "RootController.h"

@interface RootController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray<NSDictionary *> *datas;

@end

@implementation RootController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    [self setupView];
    [self setupData];
}

- (void)setupData {
    self.datas = @[
        @{@"title": @"CRC", @"class": @"DCRCController"},
        @{@"title": @"Alert", @"class": @"DAlertController"},
        @{@"title": @"Common", @"class": @"DCommonController"},
        @{@"title": @"Device", @"class": @"DDeviceController"},
        
        
    ];
    [self.tableView reloadData];
}

- (void)setupView {
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.datas.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellId = @"UITableViewCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    cell.textLabel.text = self.datas[indexPath.row][@"title"];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    NSString *className = self.datas[indexPath.row][@"class"];
    Class vcClass = NSClassFromString(className);
    if (vcClass) {
        UIViewController *vc = [[vcClass alloc] init];
        vc.title = self.datas[indexPath.row][@"title"];
        [self.navigationController pushViewController:vc animated:YES];
    } else {
        NSLog(@"未找到类: %@", className);
    }
}



@end
